﻿using System;
using System.Windows.Controls;
using HCI.Questionaire.Entities;
using HCI.Questionaire.EventArgument;
using HCI.Questionaire.QuestionRender;

namespace HCI.Questionaire.QuestionGroup
{
    public partial class QuestionGroupHeader : UserControl
    {
        #region Event
        public event EventHandler<ValidateConfirmationNotifyArgs> ValidateConfirmationNotify;
        public event Action<int> LastMoveNextInQuestionGroup;
        public event Action<int> LastMovePreviousInQuestionGroup;
        public event Action<int> LastMoveNextInQuestionGroupMouseEnter;
        public event Action<int> LastMoveNextInQuestionGroupMouseLeave;
        public event Action<int> LastPreviousMoveInQuestionGroupMouseEnter;
        public event Action<int> LastPreviousMoveInQuestionGroupMouseLeave;
        public event Action<int> CurentQuestionGroupIndexChanged;


        #region Event: Method Invoker
        private void OnCurentQuestionGroupIndexChanged(int oldIndex)
        {
            if (this.CurentQuestionGroupIndexChanged != null)
            {
                this.CurentQuestionGroupIndexChanged(oldIndex);
            }
        }
        private void OnValidateConfirmationNotify(object sender, ValidateConfirmationNotifyArgs e)
        {
            if (this.ValidateConfirmationNotify != null)
            {
                this.ValidateConfirmationNotify(sender, e);
            }
        }
        private void OnLastMoveNextInQuestionGroupMouseEnter(int questionGroupIndex)
        {
            if (this.LastMoveNextInQuestionGroupMouseEnter != null)
            {
                this.LastMoveNextInQuestionGroupMouseEnter(questionGroupIndex);
            }
        }
        private void OnLastMoveNextInQuestionGroupMouseLeave(int questionGroupIndex)
        {
            if (this.LastMoveNextInQuestionGroupMouseLeave != null)
            {
                this.LastMoveNextInQuestionGroupMouseLeave(questionGroupIndex);
            }
        }
        private void OnLastPreviousMoveInQuestionGroupMouseEnter(int questionGroupIndex)
        {
            if (this.LastPreviousMoveInQuestionGroupMouseEnter != null)
            {
                this.LastPreviousMoveInQuestionGroupMouseEnter(questionGroupIndex);
            }
        }
        private void OnLastPreviousMoveInQuestionGroupMouseLeave(int questionGroupIndex)
        {
            if (this.LastPreviousMoveInQuestionGroupMouseLeave != null)
            {
                this.LastPreviousMoveInQuestionGroupMouseLeave(questionGroupIndex);
            }
        }

        private void OnLastMoveNextInQuestionGroup(int questionGroupIndex)
        {
            if (this.LastMoveNextInQuestionGroup != null)
            {
                this.LastMoveNextInQuestionGroup(questionGroupIndex);
            }
        }

        private void OnLastMovePreviousInQuestionGroup(int questionGroupIndex)
        {
            if (this.LastMovePreviousInQuestionGroup != null)
            {
                this.LastMovePreviousInQuestionGroup(questionGroupIndex);
            }
        }
        #endregion
        #endregion

        #region Feild
        private int fuckQuestionIndex = 3;
        private IQuestionGroup[] _QuestionGroupList;
        private UserControl[] _QuestionGroupRenderControl;
        private int _CurrentQestionGroupIndex;
        #endregion

        #region Constructor
        public QuestionGroupHeader()
        {
            InitializeComponent();

            _QuestionGroupList = new IQuestionGroup[7];

            _QuestionGroupList[0] = new QuestionGroup1();
            _QuestionGroupList[1] = new QuestionGroup2();
            _QuestionGroupList[2] = new QuestionGroup3();
            _QuestionGroupList[3] = new QuestionGroup4();
            _QuestionGroupList[4] = new QuestionGroup5();
            _QuestionGroupList[5] = new QuestionGroup6();
            _QuestionGroupList[6] = new QuestionGroup7();

            _QuestionGroupRenderControl = new UserControl[7];

            _QuestionGroupRenderControl[0] = new QuestionGroup1Render();
            _QuestionGroupRenderControl[1] = new QuestionGroup2Render();
            _QuestionGroupRenderControl[2] = new QuestionGroup3Render();
            _QuestionGroupRenderControl[3] = new QuestionGroup4Render();
            _QuestionGroupRenderControl[4] = new QuestionGroup5Render();
            _QuestionGroupRenderControl[5] = new QuestionGroup6Render();
            _QuestionGroupRenderControl[6] = new QuestionGroup7Render();

            foreach (var item in this._QuestionGroupList)
            {
                item.ValidateConfirmationNotify += new EventHandler<HCI.Questionaire.EventArgument.ValidateConfirmationNotifyArgs>(QuestionGroupHeader_ValidateConfirmationNotify);
                item.AnswerInQuestionChanged += new Action<IQuestionGroup>(QuestionGroupHeader_AnswerInQuestionChanged);
            }

            this.pagerMediaPlayer.PlayClicked += new HCI.Questionaire.Controls.CallbackDelegate(pagerMediaPlayer_PlayClicked);
            this.pagerMediaPlayer.NextMouseEnter += new System.Windows.Input.MouseEventHandler(pagerMediaPlayer_NextMouseEnter);
            this.pagerMediaPlayer.NextMouseLeave += new System.Windows.Input.MouseEventHandler(pagerMediaPlayer_NextMouseLeave);
            this.pagerMediaPlayer.PreviousMouseEnter += new System.Windows.Input.MouseEventHandler(pagerMediaPlayer_PreviousMouseEnter);
            this.pagerMediaPlayer.PreviousMouseLeave += new System.Windows.Input.MouseEventHandler(pagerMediaPlayer_PreviousMouseLeave);
        }

      
        #endregion

        #region Preperty
        private int CurrentQestionGroupIndex
        {
            get
            {
                return _CurrentQestionGroupIndex;
            }
            set
            {
                this.OnCurentQuestionGroupIndexChanged(this._CurrentQestionGroupIndex);
                this._QuestionGroupList[this._CurrentQestionGroupIndex].ValidateConfirmation(new ChangeGroupCallerInfo { CallerState = "GroupIndexBeforeChange" });
                this._CurrentQestionGroupIndex = value;
            }
        }
        public IQuestionGroup CurrentQuestionGroup { get; private set; }
        public UserControl CurrentQuestionGroupRenderControl { get; private set; }
        #endregion

        #region Method
        public void LoadQuestionGroup(int questionGroup)
        {
            if (questionGroup < 0 || questionGroup > _QuestionGroupRenderControl.Length || (this.CurrentQuestionGroup != null && this.CurrentQuestionGroup.GroupIndex == questionGroup))
                return;

            this.CurrentQestionGroupIndex = questionGroup;
            // Load Question renderer
            this.CurrentQuestionGroupRenderControl = _QuestionGroupRenderControl[questionGroup];
            this.gidControlRenderContainer.Children.Clear();
            this.gidControlRenderContainer.Children.Add(this.CurrentQuestionGroupRenderControl);
            this.gidControlRenderContainer.UpdateLayout();

            // Re-assign Question group
            if (this.CurrentQuestionGroup != null)
            {
                this.CurrentQuestionGroup.QuestionIndexChanged -= new EventHandler<HCI.Questionaire.EventArgument.QuestionIndexChangedAgrs>(CurrentQuestionGroup_QuestionIndexChanged);
            }
            this.CurrentQuestionGroup = _QuestionGroupList[questionGroup];
            this.CurrentQuestionGroup.QuestionIndexChanged += new EventHandler<HCI.Questionaire.EventArgument.QuestionIndexChangedAgrs>(CurrentQuestionGroup_QuestionIndexChanged);
            ((IQuestionGroupRenderControl)this.CurrentQuestionGroupRenderControl).QuestionRunningNumber = this.CurrentQuestionGroup.QuestionStateText;

            this.RenderQuestion();
            this.AdjustNavigateButton();
            this.CurrentQuestionGroup.LoadHistoryCount++;
        }

        void CurrentQuestionGroup_QuestionIndexChanged(object sender, HCI.Questionaire.EventArgument.QuestionIndexChangedAgrs e)
        {
            this.RenderQuestion();

            var question = ((IQuestionGroup)sender);
            var canMoveNextIngroup = question.CanMoveNext;

            AdjustNavigateButton();

            ((IQuestionGroupRenderControl)this.CurrentQuestionGroupRenderControl).QuestionRunningNumber = question.QuestionStateText;
        }

        private void AdjustNavigateButton()
        {
            this.pagerMediaPlayer.SetNextEnable(!(this.CurrentQuestionGroup.GroupIndex == 6 && !this.CurrentQuestionGroup.CanMoveNext));
            this.pagerMediaPlayer.SetPreviousEnable(!(this.CurrentQuestionGroup.GroupIndex == 0 && !this.CurrentQuestionGroup.CanMovePrevious));
        }

        private void RenderQuestion()
        {
            ((IQuestionGroupRenderControl)this.CurrentQuestionGroupRenderControl).InitailRender(this.CurrentQuestionGroup);
        }
        #endregion

        #region EventHandler: QuestionGroupHeader

        void QuestionGroupHeader_ValidateConfirmationNotify(object sender, HCI.Questionaire.EventArgument.ValidateConfirmationNotifyArgs e)
        {
            OnValidateConfirmationNotify(sender, e);
        }
        void QuestionGroupHeader_AnswerInQuestionChanged(IQuestionGroup group)
        {
            this._QuestionGroupList[group.GroupIndex].ValidateConfirmation();
        }
        #endregion

        #region EventHandler: PagerMediaPlayer Control
        void pagerMediaPlayer_PreviousMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.OnLastPreviousMoveInQuestionGroupMouseLeave(this.CurrentQuestionGroup.GroupIndex);
        }

        void pagerMediaPlayer_PreviousMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (!this.CurrentQuestionGroup.CanMovePrevious)
                this.OnLastPreviousMoveInQuestionGroupMouseEnter(this.CurrentQuestionGroup.GroupIndex);
        }

        void pagerMediaPlayer_NextMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.OnLastMoveNextInQuestionGroupMouseLeave(this.CurrentQuestionGroup.GroupIndex);
        }

        void pagerMediaPlayer_NextMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (!this.CurrentQuestionGroup.CanMoveNext)
                this.OnLastMoveNextInQuestionGroupMouseEnter(this.CurrentQuestionGroup.GroupIndex);

            // OR another case
            if (this.CurrentQuestionGroup.CurrentQuestionIndex == fuckQuestionIndex && this.CurrentQuestionGroup.QuestionList[fuckQuestionIndex].AnswerCollection[2].IsAnswered)
                this.OnLastMoveNextInQuestionGroupMouseEnter(this.CurrentQuestionGroup.GroupIndex);
        }

        void pagerMediaPlayer_PlayClicked(HCI.Questionaire.Controls.CallbackEventArgs arg)
        {
            if (arg.Action == "Clicked" && arg.Value == "Previous")
            {
                if (this.CurrentQuestionGroup.CanMovePrevious)
                {
                    this.CurrentQuestionGroup.PreviousQuestion();
                }
                else
                {
                    this.OnLastMovePreviousInQuestionGroup(this.CurrentQuestionGroup.GroupIndex);
                }
            }
            else if (arg.Action == "Clicked")
            {
                if (this.CurrentQuestionGroup.CanMoveNext && !(this.CurrentQuestionGroup.GroupIndex == 0 && this.CurrentQuestionGroup.CurrentQuestionIndex == this.fuckQuestionIndex && this.CurrentQuestionGroup.QuestionList[this.fuckQuestionIndex].AnswerCollection[2].IsAnswered))
                {
                    this.CurrentQuestionGroup.NextQuestion();
                }
                else if (this.CurrentQuestionGroup.GroupIndex == 0 && this.CurrentQuestionGroup.CurrentQuestionIndex == this.fuckQuestionIndex && this.CurrentQuestionGroup.QuestionList[this.fuckQuestionIndex].AnswerCollection[2].IsAnswered)
                {
                    this.OnLastMoveNextInQuestionGroup(this.CurrentQuestionGroup.GroupIndex);
                }
                else
                {
                    this.OnLastMoveNextInQuestionGroup(this.CurrentQuestionGroup.GroupIndex);
                }
            }

            if (arg.Action == "MouseEnter" && arg.Value == "Previous")
            {
                //this.CurrentQuestionGroup.PreviousQuestion();
            }
            else if (arg.Action == "MouseEnter")
            {
                //this.CurrentQuestionGroup.NextQuestion();
            }

        }
        #endregion
    }
}